/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*   File:      AddNewAGV.cpp
*   Author:    刘洋兵
*   Function:  添加AGV小车信息界面
*   Time:      2025-09-09
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#include "AddNewAGV.h"
#include "ui_AddNewAGV.h"

#include <QDebug>

AddNewAGV::AddNewAGV(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::AddNewAGV)
{
    ui->setupUi(this);
}

AddNewAGV::~AddNewAGV()
{
    delete ui;
}

/**
* @brief  初始化界面数据
*/
void AddNewAGV::InitData()
{
    static bool is_first = true;
    QString sql = "vehicle_name,ip_address,port";
    QString dbtablename = "tb_vehicle_profile";
    QList<QStringList> dataList = MyDatabase::getInstance().query_some_tabledata(sql,dbtablename);
    int rowcount = dataList.count();
    // 清除下拉框数据
    ui->comboBox_AGVname->clear();
    ui->comboBox_ip->clear();
    ui->comboBox_port->clear();
    for (int row = 0; row < rowcount; row++) {
        if(ui->comboBox_AGVname->findText(dataList[row][0])==-1)
        {
            ui->comboBox_AGVname->addItem(dataList[row][0]);
        }
        if(ui->comboBox_ip->findText(dataList[row][1])==-1)
        {
            ui->comboBox_ip->addItem(dataList[row][1]);
        }
        if(ui->comboBox_port->findText(dataList[row][2])==-1)
        {
            ui->comboBox_port->addItem(dataList[row][2]);
        }
    }
    if(is_first)
    {
        ui->comboBox_brand->addItem("睿芯行",1);
        ui->comboBox_brand->addItem("自研",0);
        ui->comboBox_vehicle_type->addItem("顶升车",1);
        ui->comboBox_vehicle_type->addItem("搬运车",2);
        ui->comboBox_vehicle_type->addItem("堆高车",3);
        ui->comboBox_battery_type->addItem("磷酸铁锂",0);
        ui->comboBox_is_available->addItem("可用",1);
        ui->comboBox_is_available->addItem("不可用",0);
        is_first = false;
    }
}

/**
* @brief 数据发送按键功能
*/
void AddNewAGV::on_pushButton_clicked()
{
    QMap<QString, QVariant> infomap;
    QMap<QString, QVariant> dataMap;
    infomap["table_name"] = "tb_vehicle_profile";
    infomap["operation_type"] = 1;
    QStringList listdata;
    QString name = ui->comboBox_AGVname->currentText();
    QString brand = ui->comboBox_brand->currentText();
    QString ip = ui->comboBox_ip->currentText();
    QString port = ui->comboBox_port->currentText();
    QString vehicle_type = ui->comboBox_vehicle_type->currentText();
    QString battery_type = ui->comboBox_battery_type->currentText();
    QString current = ui->doubleSpinBox_charge_max_current->text();
    QString voltage = ui->doubleSpinBox_charge_max_voltage->text();
    QString is_available = ui->comboBox_is_available->currentText();

    dataMap["vehicle_id"] = -1;
    dataMap["vehicle_name"] = name;
    dataMap["brand"] = ui->comboBox_brand->currentData().toInt();
    dataMap["ip_address"] = ip;
    dataMap["port"] = port.toInt();
    dataMap["vehicle_type"] = ui->comboBox_vehicle_type->currentData().toInt();
    dataMap["battery_type"] = ui->comboBox_battery_type->currentData().toInt();
    dataMap["charge_max_current"] = current.toDouble();
    dataMap["charge_max_voltage"] = voltage.toDouble();
    dataMap["is_available"] = ui->comboBox_is_available->currentData().toInt();

    if(name.isEmpty())
    {
        return;
    }
    if(CommandSendorReceive::getInstance().isExecute("提示","确认添加小车"))
    {
        //发送json数据
        CommandSendorReceive::getInstance().sendCommand(dataMap, infomap, 202, true);
        LOG_INFO("添加小车信息");
        listdata <<" "<< name << brand << ip << port << vehicle_type << battery_type << current << voltage <<is_available;
        emit senddatalist(listdata);
    }
    close();
}
